//
//  ProductDetailView.m
//  sensen_ios
//
//  Created by pro on 16/2/24.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ProductDetailView.h"

@interface ProductDetailView ()
{
    UIView *_selectedView; // 选中视图
    NSInteger _selectedTag; // 当前选中视图的tag值
}
@end

@implementation ProductDetailView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _initViews];
    }
    
    return self;
}

#pragma mark -- 创建视图 --
- (void)_initViews {
    // ———————————— 商品详情 ————————————
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = SCREEN_WIDTH;
    CGFloat height = 64;
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    imgView.image = [UIImage imageNamed:@"图文详情图片"];
    [self addSubview:imgView];
    
    
    // 图文详情 和 店铺推荐 视图
    x = 0;
    y = imgView.bottom;
    width = self.width;
    height = 40;
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    bgView.tag = 10;
    
    NSArray *titles = @[@"图文详情", @"商城推荐"];
    width = bgView.width / 2;
    height = bgView.height;
    for (int i = 0; i < 2; i ++) {
        x = i * SCREEN_WIDTH / 2;
        y = 0;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        label.userInteractionEnabled = YES;
        label.font = FONT(14);
        label.text = titles[i];
        label.textColor = [UIColor darkGrayColor];
        label.textAlignment = NSTextAlignmentCenter;
        [bgView addSubview:label];
        label.tag = i + 1;
        // 添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [label addGestureRecognizer:tap];
    }
    
    // 中间的分割线
    width = 1.5;
    height = bgView.height * 0.6;
    UIView *v_line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    v_line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    v_line.center = CGPointMake(bgView.width / 2, bgView.height / 2);
    [bgView addSubview:v_line];
    
    // 创建选中视图
    width = bgView.width * 0.4;
    height = 1.5;
    _selectedView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _selectedView.backgroundColor = [UIColor redColor];
    [bgView addSubview:_selectedView];
    
    UILabel *label1 = (UILabel *)[bgView viewWithTag:1];
    label1.textColor = [UIColor redColor];
    _selectedView.center = CGPointMake(label1.center.x, label1.height - _selectedView.height / 2);
    
    _selectedTag = 1;
}



#pragma mark - 点击事件
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSInteger tag = tap.view.tag;
    // 点击了是当前选中的不做操作
    if (tag == _selectedTag) {
        return;
    } else {
        // 设置字体颜色
        UILabel *curSelectLabel = (UILabel *)tap.view;
        curSelectLabel.textColor = [UIColor redColor];
        
        UIView *bgView = [self viewWithTag:10];
        UILabel *prevSelectLabel = (UILabel *)[bgView viewWithTag:_selectedTag];
        prevSelectLabel.textColor = [UIColor darkGrayColor];
        
        // 设置选中视图的位置
        _selectedView.center = CGPointMake(curSelectLabel.center.x, curSelectLabel.height - _selectedView.height / 2);
        
        // 最后改变当前选中的tag值
        _selectedTag = tag;
        
        // 回调block
        if (_chooseBlock != nil) {
            _chooseBlock(_selectedTag);
        }
    }
    
}

@end
